package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.dto.BusinessTrackDto;
import com.huike.business.domain.vo.BusinessTrackVO;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessServiceImpl tbBusinessService;

    @Transactional
    @Override
    public void addBusinessTrack(BusinessTrackDto dto) {
        try {
            //获取商机id
            Long businessId = dto.getBusinessId();
            //创建商机对象传递参数
            TbBusiness tbBusiness = new TbBusiness();
            tbBusiness.setId(businessId);
            //属性拷贝
            BeanUtils.copyProperties(dto, tbBusiness);
            //根据商机id找到对应商机表，并更新商机内容
            tbBusiness.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());
            tbBusinessService.updateTbBusiness(tbBusiness);
            //补充字段
            String username = SecurityUtils.getUsername();
            dto.setCreateBy(username);
            dto.setCreateTime(DateUtils.getNowDate());
            dto.setType("O");
            //添加商机跟进记录
            tbBusinessTrackRecordMapper.add(dto);
        } catch (BeansException e) {
            throw new RuntimeException("添加商机跟进记录失败，错误原因" + e);
        }
    }

    @Override
    public List<BusinessTrackVO> getBusinessTrackList(Long id) {
        return tbBusinessTrackRecordMapper.getBusinessTrackList(id);
    }
}
